package com.bafa.objects.team;

import com.bafa.objects.Match;
import com.bafa.objects.Scorer;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

public class TeamSeasonStats {
	private int biggestDefeat;

	private int biggestDefeatMatchId;

	private int biggestWin;

	private int biggestWinMatchId;

	private int cleanSheets;

	private int gamesDrawn;

	private int gamesLost;

	private int gamesWon;

	private int goalsConceded;

	private int goalsScored;

	private int leagueGamesDrawn;

	private int leagueGamesLost;

	private int leagueGamesWon;

	private int leagueGoalsConceded;

	private int leagueGoalsScored;

	private List matches = new ArrayList();

	private int mostConceded;

	private int mostConcededMatchId;

	private int mostScored;

	private int mostScoredMatchId;

	private TreeMap scorers = new TreeMap();

	private int teamId;

	public TeamSeasonStats() {
		// TODO Auto-generated constructor stub
	}

	public TeamSeasonStats(int teamId) {
		this.teamId = teamId;
	}

	public void addMatches(int matchId) {
		matches.add(matchId);
}

	public void addScorers(String name, String othername) {
		scorers.put(name, othername);
	}
	
	public void enterMatch(Match match) {
		matches.add(match.getID());

	}

	public void enterScorer(String name) {
		if (!scorers.containsKey(name)) {
			scorers.put(name, name);
		}

	}

	public int getBiggestDefeat() {
		return biggestDefeat;
	}

	public int getBiggestDefeatMatchId() {
		return biggestDefeatMatchId;
	}

	public int getBiggestWin() {
		return biggestWin;
	}

	public int getBiggestWinMatchId() {
		return biggestWinMatchId;
	}

	public int getCleanSheets() {
		return cleanSheets;
	}

	public int getGamesDrawn() {
		return gamesDrawn;
	}

	public int getGamesLost() {
		return gamesLost;
	}

	public int getGamesWon() {
		return gamesWon;
	}

	public int getGoalsConceded() {
		return goalsConceded;
	}

	public int getGoalsScored() {
		return goalsScored;
	}

	public int getLeagueGamesDrawn() {
		return leagueGamesDrawn;
	}

	public int getLeagueGamesLost() {
		return leagueGamesLost;
	}

	public int getLeagueGamesWon() {
		return leagueGamesWon;
	}

	public int getLeagueGoalsConceded() {
		return leagueGoalsConceded;
	}

	public int getLeagueGoalsScored() {
		return leagueGoalsScored;
	}

	public List getMatches() {
		return matches;
	}

	public int getMostConceded() {
		return mostConceded;
	}

	public int getMostConcededMatchId() {
		return mostConcededMatchId;
	}

	public int getMostScored() {
		return mostScored;
	}

	public int getMostScoredMatchId() {
		return mostScoredMatchId;
	}

	public TreeMap getScorers() {
		return scorers;
	}

	public int getTeamId() {
		return teamId;
	}

	public int returnGamesPlayed(){
		return gamesDrawn+gamesWon+gamesLost;
	}

	public int returnLeagueGamesPlayed(){
		return leagueGamesDrawn+leagueGamesLost+leagueGamesWon;
	}

	public String[] scorers(){
		return (String[])this.getScorers().values().toArray(new String[this.getScorers().size()]);
	}

	public void setBiggestDefeat(int biggestDefeat) {
		this.biggestDefeat = biggestDefeat;
	}

	public void setBiggestDefeatMatchId(int biggestDefeatMatchId) {
		this.biggestDefeatMatchId = biggestDefeatMatchId;
	}

	public void setBiggestWin(int biggestWin) {
		this.biggestWin = biggestWin;
	}

	public void setBiggestWinMatchId(int biggestWinMatchId) {
		this.biggestWinMatchId = biggestWinMatchId;
	}

	public void setCleanSheets(int cleanSheets) {
		this.cleanSheets = cleanSheets;
	}

	public void setGamesDrawn(int gamesDrawn) {
		this.gamesDrawn = gamesDrawn;
	}

	public void setGamesLost(int gamesLost) {
		this.gamesLost = gamesLost;
	}

	public void setGamesWon(int gamesWon) {
		this.gamesWon = gamesWon;
	}

	public void setGoalsConceded(int goalsConceded) {
		this.goalsConceded = goalsConceded;
	}

	public void setGoalsScored(int goalsScored) {
		this.goalsScored = goalsScored;
	}

	public void setLeagueGamesDrawn(int leagueGamesDrawn) {
		this.leagueGamesDrawn = leagueGamesDrawn;
	}

	public void setLeagueGamesLost(int leagueGamesLost) {
		this.leagueGamesLost = leagueGamesLost;
	}

	public void setLeagueGamesWon(int leagueGamesWon) {
		this.leagueGamesWon = leagueGamesWon;
	}

	public void setLeagueGoalsConceded(int leagueGoalsConceded) {
		this.leagueGoalsConceded = leagueGoalsConceded;
	}

	public void setLeagueGoalsScored(int leagueGoalsScored) {
		this.leagueGoalsScored = leagueGoalsScored;
	}

	public void setMatches(ArrayList matches) {
		this.matches = matches;
	}

	public void setMostConceded(int mostConceded) {
		this.mostConceded = mostConceded;
	}

	public void setMostConcededMatchId(int mostConcededMatchId) {
		this.mostConcededMatchId = mostConcededMatchId;
	}

	public void setMostScored(int mostScored) {
		this.mostScored = mostScored;
	}

	public void setMostScoredMatchId(int mostScoredMatchId) {
		this.mostScoredMatchId = mostScoredMatchId;
	}
	
	public void setScorers(TreeMap scorers) {
		this.scorers = scorers;
	}
	
	public void setTeamId(int teamId) {
		this.teamId = teamId;
	}
	
	/**
	 * Refresh the info
	 * 
	 * @param match
	 */
	public void updateScores(Match match) {
		// FIrstly check to see home or away
		boolean home = (match.getTeamA() == teamId);

		if (home) {

			// add league information and stats
			if (match.getDivision() > 0) {
				leagueGoalsScored += match.getGoalsA();
				leagueGoalsConceded += match.getGoalsB();
				if (match.getGoalsA() > match.getGoalsB()) {
					leagueGamesWon += 1;
				} else if (match.getGoalsA() < match.getGoalsB()) {
					leagueGamesLost += 1;
				} else {
					leagueGamesDrawn += 1;
				}
			}
			// add total stats
			goalsScored += match.getGoalsA();
			goalsConceded += match.getGoalsB();
			if (match.getGoalsB() == 0)
				cleanSheets += 1;
			if (match.getGoalsA() > match.getGoalsB()) {
				gamesWon += 1;
			} else if (match.getGoalsA() < match.getGoalsB()) {
				gamesLost += 1;
			} else {
				gamesDrawn += 1;
			}

			if (match.getGoalsA() - match.getGoalsB() > biggestWin) {
				biggestWinMatchId = match.getID();
				biggestWin = match.getGoalsA() - match.getGoalsB();
			}
			if (match.getGoalsB() - match.getGoalsA() > biggestDefeat) {
				biggestDefeatMatchId = match.getID();
				biggestDefeat = match.getGoalsB() - match.getGoalsA();
			}
			if (match.getGoalsA() > mostScored) {
				mostScoredMatchId = match.getID();
				mostScored = match.getGoalsA();
			}
			if (match.getGoalsB() > mostConceded) {
				mostConcededMatchId = match.getID();
				mostConceded = match.getGoalsB();
			}

		} else {
			if (match.getDivision() > 0) {
				leagueGoalsScored += match.getGoalsB();
				leagueGoalsConceded += match.getGoalsA();
				if (match.getGoalsB() > match.getGoalsA()) {
					leagueGamesWon += 1;
				} else if (match.getGoalsB() < match.getGoalsA()) {
					leagueGamesLost += 1;
				} else {
					leagueGamesDrawn += 1;
				}
			}

			goalsScored += match.getGoalsB();
			goalsConceded += match.getGoalsA();
			if (match.getGoalsA() == 0)
				cleanSheets += 1;
			if (match.getGoalsB() > match.getGoalsA()) {
				gamesWon += 1;
			} else if (match.getGoalsB() < match.getGoalsA()) {
				gamesLost += 1;
			} else {
				gamesDrawn += 1;
			}

			if (match.getGoalsB() - match.getGoalsA() > biggestWin) {
				biggestWinMatchId = match.getID();
				biggestWin = match.getGoalsB() - match.getGoalsA();
			}
			if (match.getGoalsA() - match.getGoalsB() > biggestDefeat) {
				biggestDefeatMatchId = match.getID();
				biggestDefeat = match.getGoalsA() - match.getGoalsB();
			}
			if (match.getGoalsB() > mostScored) {
				mostScoredMatchId = match.getID();
				mostScored = match.getGoalsB();
			}
			if (match.getGoalsA() > mostConceded) {
				mostConcededMatchId = match.getID();
				mostConceded = match.getGoalsA();
			}

		}
	}
}
